cmake_minimum_required(VERSION 3.5)

project(NetworkTest)

set(CMAKE_CXX_STANDARD 11)
set(CMAKE_CXX_STANDARD_REQUIRED ON)

#set(CMAKE_BUILD_TYPE Debug)
set(CMAKE_BUILD_TYPE Release)

set(PROJECT_LIB_NAME networkutil)

#指定编译器，在执行cmake指令的时候，增加一个输入，-DBUILD_PLATFORM=arm
if(${BUILD_PLATFORM} MATCHES "arm")
    message("-- Build under the arm platform")
    set(CMAKE_SYSTEM_NAME Linux)
    set(CMAKE_SYSTEM_PROCESSOR $ENV{ARCH})
    set(ARM_TOOLCHAIN_PREFIX "/opt/gcc-linaro-6.5.0-2018.12-x86_64_aarch64-linux-gnu")
    set(CMAKE_FIND_ROOT_PATH ${ARM_TOOLCHAIN_PREFIX})
    set(CMAKE_C_COMPILER ${ARM_TOOLCHAIN_PREFIX}/bin/aarch64-linux-gnu-gcc)
    set(CMAKE_CXX_COMPILER ${ARM_TOOLCHAIN_PREFIX}/bin/aarch64-linux-gnu-g++)
    set(CMAKE_FIND_ROOT_PATH_MODE_PROGRAM NEVER)
    set(CMAKE_FIND_ROOT_PATH_MODE_LIBRARY ONLY)
    set(CMAKE_FIND_ROOT_PATH_MODE_INCLUDE ONLY)
    set(CMAKE_FIND_ROOT_PATH_MODE_PACKAGE ONLY)
else()
    message("-- Build under the x86 platform")
    set(CMAKE_CXX_COMPILER g++)
    set(CMAKE_C_COMPILER gcc)
endif()


if(WIN32)
    set(CMAKE_C_FLAGS "-std=gnu99 -g -Wno-attributes")
    set(CMAKE_CXX_FLAGS "-std=c++11 -g -Wno-attributes")
    set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -Wl,--stack,1024000")
else()
    set(CMAKE_C_FLAGS "-std=gnu99 -g -rdynamic -Wno-attributes")
    set(CMAKE_CXX_FLAGS "-std=c++11 -g -rdynamic -Wno-attributes")
endif()


set(CMAKE_VERBOSE_MAKEFILE on)

file(GLOB_RECURSE ALL_HEADERS
    ${PROJECT_SOURCE_DIR}/src/include/*.h
    ${PROJECT_SOURCE_DIR}/src/include/*.hpp
    )

file(GLOB_RECURSE ALL_SOURCES 
    ${PROJECT_SOURCE_DIR}/src/source/*.cpp
    ${PROJECT_SOURCE_DIR}/src/source/*.cxx
    ${PROJECT_SOURCE_DIR}/src/source/*.cc
    ${PROJECT_SOURCE_DIR}/src/source/*.c
    )
	
file(GLOB_RECURSE TEST_SOURCES 
    ${PROJECT_SOURCE_DIR}/src/main.cpp
    )


include_directories(${PROJECT_SOURCE_DIR}/inc)
include_directories(${PROJECT_SOURCE_DIR}/src)
include_directories(${PROJECT_SOURCE_DIR}/src/include)


if(${BUILD_PLATFORM} MATCHES "arm")
    message("-- Current system name: Linux-arm")
    set(LIBRARY_OUTPUT_PATH ${PROJECT_SOURCE_DIR}/bin/arm)
    add_library(${PROJECT_LIB_NAME} SHARED ${ALL_SOURCES} ${ALL_HEADERS})
    target_link_libraries(
        ${PROJECT_LIB_NAME}
        -lpthread
        -lrt
        -lm)
	
    set(EXECUTABLE_OUTPUT_PATH ${PROJECT_SOURCE_DIR}/bin/arm)
    add_executable(${PROJECT_NAME} ${TEST_SOURCES})
    target_link_libraries(
        ${PROJECT_NAME}
        ${PROJECT_LIB_NAME}
        -lpthread
        -lrt
        -lm)
		
    #install(DIRECTORY ${PROJECT_SOURCE_DIR}/lib/arm/ DESTINATION ${PROJECT_SOURCE_DIR}/../bin/arm)
else()
    if(CMAKE_SYSTEM_NAME MATCHES "Windows")
        #not invalid in next config: if(CMAKE_CL_64) ... else(CMAKE_CL_64) ... endif(CMAKE_CL_64)
        message("-- Current make program: ${CMAKE_MAKE_PROGRAM}")
        if(CMAKE_MAKE_PROGRAM MATCHES "_64")
            message("-- Current system name: Windows-x64")
            set(LIBRARY_OUTPUT_PATH ${PROJECT_SOURCE_DIR}/bin/win/x64)
            add_library(${PROJECT_LIB_NAME} SHARED ${ALL_SOURCES} ${ALL_HEADERS})
            target_link_libraries(
                ${PROJECT_LIB_NAME}
                -lws2_32
                -lwsock32
                -lpthread)
				
            set(EXECUTABLE_OUTPUT_PATH ${PROJECT_SOURCE_DIR}/bin/win/x64)
            add_executable(${PROJECT_NAME} ${TEST_SOURCES})
            target_link_libraries(
                ${PROJECT_NAME}
                ${PROJECT_LIB_NAME}
                -lws2_32
                -lwsock32
                -lpthread)
        else()
            message("-- Current system name: Windows-x86")
            set(LIBRARY_OUTPUT_PATH ${PROJECT_SOURCE_DIR}/bin/win/x86)
            add_library(${PROJECT_LIB_NAME} SHARED ${ALL_SOURCES} ${ALL_HEADERS})
            target_link_libraries(
                ${PROJECT_LIB_NAME}
                -lws2_32
                -lwsock32
                -lpthread)
				
            set(EXECUTABLE_OUTPUT_PATH ${PROJECT_SOURCE_DIR}/bin/win/x86)
            add_executable(${PROJECT_NAME} ${TEST_SOURCES})
            target_link_libraries(
                ${PROJECT_NAME}
                ${PROJECT_LIB_NAME}
                -lws2_32
                -lwsock32
                -lpthread)
        endif()

        #if(CMAKE_CL_64)
        #    message("-- Current system name: Windows-x64")
        #else(CMAKE_CL_64)
        #    message("-- Current system name: Windows-x86")
        #endif(CMAKE_CL_64)
    else()
        message("-- Current system name: Linux-x86_64")
        set(LIBRARY_OUTPUT_PATH ${PROJECT_SOURCE_DIR}/bin/linux)
        add_library(${PROJECT_LIB_NAME} SHARED ${ALL_SOURCES} ${ALL_HEADERS})
        target_link_libraries(
            ${PROJECT_LIB_NAME}
            -lpthread
            -lrt
            -lm)
				
        set(EXECUTABLE_OUTPUT_PATH ${PROJECT_SOURCE_DIR}/bin/linux)
        add_executable(${PROJECT_NAME} ${TEST_SOURCES})
        target_link_libraries(
            ${PROJECT_NAME}
            ${PROJECT_LIB_NAME}
            -lpthread
            -lrt
            -lm)
    endif()
endif()



